<?php

namespace app\admin\controller;

use app\AdminController;
use app\admin\logic\User;
use think\App;
use think\captcha\facade\Captcha;
use think\facade\View;

class Login extends AdminController
{
    public function index()
    {
        if (request()->isPost()) {
            $post = request()->post();
            $rememberme = true; # 已默认长时间记住
            $result = $this->validate($post, 'User.signin');
            if (true !== $result) {
                $this->error($result);
            }
            $uid = User::login($post['username'], $post['password'], $rememberme);
            if ($uid[0] != 0) {
                $this->success('登录成功', url('/console/user'));
            } else {
                $this->error($uid[1]);
            }
        } else {
            if ($this->is_signin()) {
                $this->redirect(url('/console/user'));
            } else {
                return View::fetch();
            }
        }
    }

    public function loginout()
    {
        session(null);
        cookie('uid', null);
        cookie('signin_token', null);
        $this->redirect(url('/login/index'));
    }

    public function xlogin(){
        return View::fetch('x-login');
    }

}